package com.jiang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiang.dto.request.DeptForm;
import com.jiang.dto.response.DeptResponse;
import com.jiang.model.Dept;
import com.jiang.dto.request.DeptCondition;
import com.jiang.service.DeptService;
import com.jiang.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author jason
 * @since 2019-03-18
 */
@RestController
@RequestMapping("/dept")
@Api(tags="部门操作控制层")
public class DeptController {

	private static final Logger logger = LoggerFactory.getLogger(DeptController.class);

	@Autowired
	private DeptService deptService;

	@PostMapping("/findbyCondition/{page}/{size}")
	@ApiOperation(value = "分页根据条件查询部门")
	public JsonResult<PageUtils<DeptResponse>> findAll(@RequestBody DeptCondition dept, @PathVariable int page, @PathVariable int size){
		IPage<Dept> result = deptService.page(new Page<>(page,size),new QueryWrapper<Dept>(DtoConvert.convert(dept,new Dept())));
		return JsonResult.success(PageUtils.wrapPage(result));
	}

	@PostMapping("/add")
	@ApiOperation(value = "添加部门")
	public JsonResult add(@RequestBody DeptForm model){
		Dept insertDept = DtoConvert.convert(model, new Dept());
		insertDept.preInsert();
		ValidateUtils.check(model.validate(false));
		deptService.save(insertDept);
		return JsonResult.success();
	}

	@PutMapping("/update")
	@ApiOperation("更新部门")
	public JsonResult update(@RequestBody DeptForm dept){
		Dept updateDept = DtoConvert.convert(dept, new Dept());
		updateDept.preUpdate();
		deptService.updateById(updateDept);
		return JsonResult.success();
	}

	@DeleteMapping("/{deptId}")
	@ApiOperation("删除部门")
	public JsonResult delete(@PathVariable Integer deptId){
		deptService.removeById(deptId);
		return JsonResult.success();
	}
}

